package cate.game.role.pvp.guildwar;

import cate.common.table.d.GDFunc;
import cate.common.table.d.GDGuildWar;
import cate.common.table.d.GDOperation;
import cate.common.table.pvp.guildwar.row.GuildWarDifficultyRow;
import cate.game.pvp.guildwar.data.GuildWarLog;
import cate.game.pvp.guildwar.data.base.GuildWarKV;
import cate.game.res.MixRes;
import cate.game.res.MixResAdder;
import cate.game.role.Role;
import cate.game.role.farm.AbstractFarm;
import cate.game.role.farm.PlayTimes;
import cate.game.role.pvp.guildwar.msg.GuildWarAdmireResp;
import cate.game.role.pvp.guildwar.msg.GuildWarBoxResp;
import cate.game.role.pvp.guildwar.msg.RoleGuildWarDataResp;
import cate.game.role.res.guild.po.GuildPO;
import com.fasterxml.jackson.annotation.JsonIgnore;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;
import easy.java.practice.errorcode.EcResult;

import java.util.ArrayList;
import java.util.List;

@NoteClass("公会战个人数据")
public class RoleGuildWar extends AbstractFarm {

	@NoteField("赛季")
	public int season;

	@NoteField("日志")
	public List<GuildWarLog> logs;

	@NoteField("挑战次数")
	public PlayTimes playTimes;

	@NoteField("是否领取过奖励")
	public boolean gotReward;

	@NoteField("今日是否膜拜过")
	public boolean admire;

	@NoteField("赛季功勋")
	public long point;


	@Override
	public void initialize(Role role) {
		super.initialize(role);
		if (logs == null) {
			logs = new ArrayList<>();
		}
		if (playTimes == null) {
			playTimes = new PlayTimes();
		}
		playTimes.initialize();
	}

	@Override
	public void onEnter() {
		super.onEnter();
		checkSeason();
	}

	@Override
	public void onDaySpan(boolean silence) {
		logs.clear();
		playTimes.resetAll();
		gotReward = false;
		admire = false;
		checkSeason();
		if (!silence) {
			noticeUpdate();
		}
	}

	private void checkSeason() {
		int curSeason = getKV().index;
		if (season != curSeason) {
			season = curSeason;
			point = 0;
		}
	}

	public void noticeUpdate(){
		role.sendNow(new RoleGuildWarDataResp(this));
	}

	@Override
	public int getFuncId() {
		return GDFunc.GuildFunc.WAR;
	}

	@Override
	public void onAfterOpen() {
	}

	@Override
	public int getFightFuncId() {
		return GDFunc.GuildFunc.WAR;
	}

	@JsonIgnore
	private GuildWarKV getKV(){
		return role.getGame().pvp.guildWar.getDataManager().getKv();
	}


	public EcResult<Void> challenge(int ptId, int difficulty,int star) {
		EcResult<Void> r = new EcResult();
		if (getKV().timeTable.period != GDGuildWar.Period.FIGHT) {
			return r.fail("公会战已结束");
		}
		GuildPO guild = role.getRes().guild.getGuild();
		if (guild == null) {
			return r.fail("尚未加入公会");
		}
		if (!guild.war.hasMatch()) {
			return r.fail("公会战尚未开启");
		}
		GuildWarDifficultyRow difficultyRow = role.getGame().table.guildWar.difficulty.get(difficulty);
		if (difficultyRow == null) {
			return r.fail("挑战难度不存在");
		}
		if (fighting) {
			return r.fail("正在战斗中");
		}
		EcResult<Void> r_times = this.playTimes.available(role, getFightFuncId());
		if (!r_times.ok()) {
			return r_times;
		}
		r = guild.war.challenge(role, ptId, difficulty, star);
		if (!r.ok()) {
			return r;
		}
		this.playTimes.added();
		noticeUpdate();
		fighting = true;
		return r.success();
	}

	public EcResult<Void> getBoxRwd(int pos){
		EcResult<Void> r = new EcResult();
		if (gotReward) {
			return r.fail("已领过奖励");
		}
		if (getKV().timeTable.period != GDGuildWar.Period.SETTLE_RWD) {
			return r.fail("当前已过领奖阶段");
		}
		GuildPO guild = role.getRes().guild.getGuild();
		if (guild == null) {
			return r.fail("尚未加入公会");
		}
		if (!guild.war.hasMatch()) {
			return r.fail("公会未参加比赛");
		}
		r = guild.war.field.getBoxRwd(role,pos);
		if(!r.ok()){
			return r;
		}
		gotReward = true;
		role.send(new GuildWarBoxResp(guild.war));
		noticeUpdate();
		return r.success();
	}

	public EcResult<Void> admire(int season) {
		EcResult<Void> r = new EcResult();
		if (admire) {
			return r.fail("已点赞过");
		}
		role.getGame().pvp.guildWar.getMessagingModule().admireChampion(season);
		new MixResAdder().setRes(new MixRes(GDGuildWar.ADMIRE_RWD_STR)).setOperation(GDOperation.GUILD_WAR_ADMIRE).
				setDialogReward(true).exe(role);
		admire = true;
		role.send(new GuildWarAdmireResp(season));
		noticeUpdate();
		return r;
	}

	public void addLog(GuildWarLog log){
		this.logs.add(log);
	}

	public void addPoint(long point){
		this.point+=point;
	}





}
